package com.weixin.web;

import com.agent.util.MyPropertiesConfigurer;
import com.weixin.util.WXUtil;
import com.weixin.util.WxepConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by hg on 2019/4/15.
 */

@Controller
@RequestMapping(value = "/weixin/zboxService/")
@Slf4j
public class ZboxService {

    /**
     * 获取wx.config中所需要的参数
     */
    @RequestMapping(value = "getSignature",method = RequestMethod.GET)
    @ResponseBody
    public Map getSignature(@RequestParam String url1, Model model,HttpServletRequest request, HttpServletResponse response) {
        Integer nonceStr =(int)(Math.random()*10000);
        Long timestamp = new Date().getTime();
        String accessToken = WXUtil.getToken(WxepConstants.CorpId,WxepConstants.REPAIRE_SECRET);
        String url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token="+accessToken;
        String jsapiTicket = WXUtil.httpRequest(url,"GET","").getString("ticket");
        url1 = url1.substring(0,url1.indexOf("#")==-1?url1.length():url1.indexOf("#"));

        String signature = DigestUtils.sha1Hex("jsapi_ticket="+jsapiTicket+"&noncestr="+nonceStr+"&timestamp="+timestamp+"&url="+url1);

        Map<String,Object> data = new HashMap<>();
        data.put("appId", WxepConstants.CorpId);
        data.put("timestamp",timestamp);
        data.put("nonceStr", nonceStr.toString());
        data.put("signature", signature);
        return data;
    }
}
